//DOM结构加载完毕
$(document).ready(function () {
    console.log("ready", isVCenter())
    if (isVCenter()) {
        // 隐藏通知
        let $vsc1 = $("vsc-global-notification-bar");
        console.log($vsc1);
        //$vsc1.hide();

        //减小header高度(有时候不生效)
        setVSCHeader();
    }
    fixSsoErr()
});

// 页面渲染完成后的操作（如图片、样式表、脚本文件等）加载完成后
$(window).on("load", function () {
    console.log("load", isVCenter())
    if (isVCenter()) {
        //减小header高度
        setVSCHeader();
        domObserver();
        taskToggle();

        for (let i = 0; i < 10; i++) {
            setTimeout(function () {
                domObserver();
                //调整导航栏
                fixNav()
            }, 200 * i)
        }
    }
});

function isVCenter() {
    if ($("vsc-app-main-layout").length > 0) {
        return true
    }
    if ($(document).attr('title').startsWith("vSphere")) {
        return true
    }
    return false
}

// 底部导航栏切换时减小header高度
function taskToggle() {
    $("vsc-panel-toggle .btn-trigger").on("click", function () {
        console.log("btn-trigger");
        let expand = $("#toggle-bottom-panel .btn-trigger").attr("aria-expanded");
        console.log(expand)
        if (expand === "true") {
            hideHeader()
        } else {
            showHeader()
        }
    })
}

let debounceFunc = throttle(function () {
    //一定要判断是否已处理，否则无限重复处理后和触发dom变更事件
    fixNav()
    findThink()
}, 1);

function domObserver() {
    // 创建一个MutationObserver对象
    var observer = new MutationObserver(function (mutations) {
        //一定要防抖或者判断是否已处理，否则不断重复处理后和触发dom变更事件
        debounceFunc()
        console.log("div变化")
        mutations.forEach(function (mutation) {
            let target1 = mutation.target;
            if (!target1 || !target1.classList) {
                return
            }
            if (target1.classList.contains("ng-star-inserted")) {

            }
        });
    });

// 配置MutationObserver要观察的目标
    var target = document.querySelector('body');
    var config = {childList: true, subtree: true};

// 开始观察DOM的变化
    observer.observe(target, config);
}

//减小header高度
function setVSCHeader() {
    let $head2 = $("vsc-header header");
    console.log($head2);
    $head2.css({"height": "2rem"});
}

function showHeader() {
    let $head2 = $("vsc-header header");
    console.log($head2);
    $head2.css({"height": "2rem"});
}

function hideHeader() {
    let $head2 = $("vsc-header header");
    console.log($head2);
    $head2.css({"height": "0.4rem"});
}

// 调整导航栏，放到虚拟机名称后面
function fixNav() {
    if ($(".btn-action-container #chao vsc-primary-tabs").length > 0) {
        return
    }
    let $main = $("vsc-primary-tabs");
    if ($main.length > 0) {
        let $span = $("<span id='chao'></span>")
        $span.append($main.parent())
        $(".btn-action-container").append($span)
    }
}

// 自动切换到精简置备
function findThink() {
    let $select = $("select:contains('厚置备')");
    if ($select.length <= 0) {
        return
    }
    console.log("发现’厚置备‘选项，切换到精简置备");

    // $select.val("5: thin").trigger('change');
    // $select.find("option:contains('精简置备')").attr("selected", true);
    // $select.find("option:contains('精简置备')").click()

}

// vcenter登录界面错误自动跳转到ui页面
function fixSsoErr() {
    if (window.location.pathname === "/ui/saml/websso/sso") {
        setTimeout(function () {
            window.location.pathname = "/ui/"
        }, 1000)
    }
}